function preParseMention(elem) {
    const hasAttributes = elem.getAttributeNames().some(name => ['filed', 'step_index', 'tbl_category', 'index'].includes(name));

    // 检查 table 元素的子元素是否包含特定属性
    function checkTableChildrenForAttributes(tableElem) {
        for (let i = 0; i < tableElem.childNodes.length; i++) {
            const childNode = tableElem.childNodes[i];
            if (childNode.nodeType === Node.ELEMENT_NODE) {
                if (childNode.tagName.toLowerCase() === 'tr') {
                    if (childNode.getAttributeNames().some(name => ['filed', 'step_index', 'tbl_category', 'index'].includes(name))) {
                        return true;
                    }
                    // 递归检查 tr 的子元素
                    if (checkTableChildrenForAttributes(childNode)) {
                        return true;
                    }
                } else {
                    // 递归检查其他子元素
                    if (checkTableChildrenForAttributes(childNode)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    // 特殊处理 ol 和 li 元素
    if (elem.tagName.toLowerCase() === 'ol') {
        let el = elem;
        const attributes = {};
        el.getAttributeNames().forEach(attr => {
            attributes[attr] = el.getAttribute(attr);
        });
        let mention = document.createElement('mention');
        mention.setAttribute('myTagName', 'ol');
        const attributesJson = JSON.stringify(attributes);
        mention.setAttribute('allAttributes', attributesJson);
        // 为 ol 添加样式，确保显示数字列表并调整缩进
        mention.setAttribute('style', 'list-style-type: decimal; padding-left: 40px; margin-left: 20px;');

        for (let i = 0; i < el.childNodes.length; i++) {
            const childNode = el.childNodes[i];
            if (childNode.nodeType === Node.ELEMENT_NODE) {
                const newChild = preParseMention(childNode);
                mention.appendChild(newChild);
            } else if (childNode.nodeType === Node.TEXT_NODE) {
                mention.appendChild(document.createTextNode(childNode.nodeValue != 'undefined' ? childNode.nodeValue : ''));
            } else {
                mention.appendChild(childNode.cloneNode(true));
            }
        }
        return mention;
    }

    if (elem.tagName.toLowerCase() === 'li') {
        let el = elem;
        const attributes = {};
        el.getAttributeNames().forEach(attr => {
            attributes[attr] = el.getAttribute(attr);
        });
        let mention = document.createElement('mention');
        mention.setAttribute('myTagName', 'li');
        const attributesJson = JSON.stringify(attributes);
        mention.setAttribute('allAttributes', attributesJson);

        // 为 li 添加样式，确保正确缩进
        mention.setAttribute('style', 'margin-left: 20px; padding-left: 10px;');

        for (let i = 0; i < el.childNodes.length; i++) {
            const childNode = el.childNodes[i];
            if (childNode.nodeType === Node.ELEMENT_NODE) {
                const newChild = preParseMention(childNode);
                mention.appendChild(newChild);
            } else if (childNode.nodeType === Node.TEXT_NODE) {
                mention.appendChild(document.createTextNode(childNode.nodeValue != 'undefined' ? childNode.nodeValue : ''));
            } else {
                mention.appendChild(childNode.cloneNode(true));
            }
        }
        return mention;
    }

    if (elem.tagName.toLowerCase() === 'table') {
        if (hasAttributes || checkTableChildrenForAttributes(elem)) {
            let el = elem;
            const attributes = {};
            el.getAttributeNames().forEach(attr => {
                attributes[attr] = el.getAttribute(attr);
            });
            let tagName = el.tagName.toLowerCase();
            let mention = document.createElement('mention');
            mention.setAttribute('myTagName', tagName);
            const attributesJson = JSON.stringify(attributes);
            mention.setAttribute('allAttributes', attributesJson);

            for (let i = 0; i < el.childNodes.length; i++) {
                const childNode = el.childNodes[i];
                if (childNode.nodeType === Node.ELEMENT_NODE) {
                    const newChild = preParseMention(childNode);
                    mention.appendChild(newChild);
                } else if (childNode.nodeType === Node.TEXT_NODE) {
                    mention.appendChild(document.createTextNode(childNode.nodeValue != 'undefined' ? childNode.nodeValue : ''));
                } else {
                    mention.appendChild(childNode.cloneNode(true));
                }
            }
            return mention;
        }
    }

    if (hasAttributes) {
        let el = elem;
        const attributes = {};
        el.getAttributeNames().forEach(attr => {
            attributes[attr] = el.getAttribute(attr);
        });
        let tagName = el.tagName.toLowerCase();
        let mention = document.createElement('mention');
        mention.setAttribute('myTagName', tagName);
        const attributesJson = JSON.stringify(attributes);
        mention.setAttribute('allAttributes', attributesJson);

        for (let i = 0; i < el.childNodes.length; i++) {
            const childNode = el.childNodes[i];
            if (childNode.nodeType === Node.ELEMENT_NODE) {
                const newChild = preParseMention(childNode);
                mention.appendChild(newChild);
            } else if (childNode.nodeType === Node.TEXT_NODE) {
                mention.appendChild(document.createTextNode(childNode.nodeValue != 'undefined' ? childNode.nodeValue : ''));
            } else {
                mention.appendChild(childNode.cloneNode(true));
            }
        }
        return mention;
    } else {
        let el = elem;
        let newElement = document.createElement(el.tagName);

        el.getAttributeNames().forEach(attr => {
            // 验证属性名是否有效
            if (/^[a-zA-Z_][a-zA-Z0-9\-_]*$/.test(attr)) {
                newElement.setAttribute(attr, el.getAttribute(attr));
            } else {
                console.warn(`Invalid attribute name: ${attr}`);
            }
        });

        for (let i = 0; i < el.childNodes.length; i++) {
            const childNode = el.childNodes[i];
            if (childNode.nodeType === Node.ELEMENT_NODE) {
                const newChild = preParseMention(childNode);
                newElement.appendChild(newChild);
            } else if (childNode.nodeType === Node.TEXT_NODE) {
                newElement.appendChild(document.createTextNode(childNode.nodeValue != 'undefined' ? childNode.nodeValue : ''));
            } else {
                newElement.appendChild(childNode.cloneNode(true));
            }
        }
        return newElement;
    }
}

export const preHtmlConf = {
    selector: '*',
    preParseHtml: preParseMention,
};